package com.dousb.cloudlibrary.controller;

import com.dousb.cloudlibrary.common.BaseResponse;
import com.dousb.cloudlibrary.common.CaptchaResult;
import com.dousb.cloudlibrary.common.ResultUtils;
import com.dousb.cloudlibrary.utils.CacheUtil;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class KaptchaController {

    @Resource
    private CacheUtil cacheUtil;

//    /**
//     * 生成验证码
//     *
//     * @param httpServletRequest
//     * @param httpServletResponse
//     * @throws Exception
//     */
//    @GetMapping("/kaptcha")
//    public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//        httpServletResponse.setHeader("Cache-Control", "no-store");
//        httpServletResponse.setHeader("Pragma", "no-cache");
//        httpServletResponse.setDateHeader("Expires", 0);
//        httpServletResponse.setContentType("image/gif");
//
//        //生成验证码对象,三个参数分别是宽、高、位数
//        SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
//        //设置验证码的字符类型为数字和字母混合
//        captcha.setCharType(Captcha.TYPE_DEFAULT);
//        // 设置内置字体
//        captcha.setCharType(Captcha.FONT_1);
//        //验证码存入session
//        httpServletRequest.getSession().setAttribute("verifyCode", captcha.text().toLowerCase());
//        log.info("验证码：{}", httpServletRequest.getSession().getAttribute("verifyCode"));
//        //输出图片流
//        captcha.out(httpServletResponse.getOutputStream());
//    }

    @GetMapping
    public BaseResponse<CaptchaResult> verifyCode(HttpServletRequest request, HttpServletResponse response) {
        //生成验证码对象
        SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
        //设置验证码的字符类型为数字和字母混合
        captcha.setCharType(Captcha.TYPE_DEFAULT);
        // 设置内置字体
        captcha.setCharType(Captcha.FONT_1);

        //生成唯一标识 作为redis的key
        String captchaKey = "captcha:" + UUID.randomUUID();
        //获取验证码文本
        String captchaText = captcha.text().toLowerCase();
        //将验证码存入redis
        cacheUtil.set(captchaKey, captchaText, 5, TimeUnit.MINUTES);
        //验证码换base64
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        captcha.out(outputStream);
        String base64Image = "data:image/gif;base64," + Base64.getEncoder().encodeToString(outputStream.toByteArray());
        log.info("生成验证码，key: {}，value: {}", captchaKey, captchaText);

        // 返回验证码key和图片Base64
        CaptchaResult result = new CaptchaResult();
        result.setCaptchaKey(captchaKey);
        result.setCaptchaImage(base64Image);
        return ResultUtils.success(result);
    }
}
